< previous page page_553 next page >

Page 553
(table continued from previous page)
ControlPropertySetting
DatabaseNameC:\TYSDBVB5\SOURCE\DATA\ERRORS\
ERRORDB.MDB
Height360
RecordSource"Table1"
Top975
Width4665
VB.LabelNamelblName
Caption"Name"
Height255
Left120
Top540
Width1215
VB.LabelNamelblKeyField
Caption"Key Field"
Height255
Left120
Top120
Width1215

The only code you need to add to this form is a single line to support the Add button. Place the following code behind the cmdAdd_Click event.
Private Sub cmdAdd_Click()
   Data1.Recordset.AddNew
End Sub
Now save the new form as DATAERR.FRM and the project as DATAERR.VBP. When you run the project, you can test the built-in error trapping for Microsoft data controls by adding a new, duplicate record to the table. Press the Add button, then enter KF109 in the KeyField input box and press one of the arrows on the data control to force it to save the record. You should see a database error message that looks like the one in Figure 14.12.
Are you surprised? You didn't add an error trap to the data entry form, but you still got a complete database error message! The Visual Basic data control is kind enough to provide complete database error reporting even if you have no error handlers in your Visual Basic program. Along with the automatic errors, the data control also has the Error event. Each time a data-related error occurs, this event occurs. You can add code in the Data1_Error event to automatically fix errors, display better error messages, and so on.

 
< previous page page_553 next page >

If you like this book, buy it!